import * as echarts from 'echarts'

// 初始化柱状图
var titleList = [
  'A类逾期占比',
  'B类逾期占比',
  'C类逾期占比',
  'D类逾期占比',
  'E类逾期占比',
]
export const initBarEcharts = (dom, list, color) => {
  var myChart = echarts.init(dom, null, {
    renderer: 'canvas',
    useDirtyRect: false,
  })
  var data = list
  // list.forEach((item, index) => {
  //   if ($(dom).attr('id') == 'bar7') {
  //     data.push(item.rate7)
  //   } else if ($(dom).attr('id') == 'bar30') {
  //     data.push(item.rate30)
  //   } else {
  //     data.push(item.rate90)
  //   }
  // })
  var option = {
    grid: {
      left: 80,
      right: 0,
      bottom: 20,
    },
    xAxis: {
      type: 'category',
      axisTick: {
        show: false,
      },
      axisLine: {
        lineStyle: {
          color: '#DBDBDB',
        },
      },
      axisLabel: {
        color: '#2D2D2D',
      },
      data: titleList,
    },
    yAxis: {
      type: 'value',
      axisLabel: {
        formatter: '{value}%',
        color: '#2D2D2D',
      },
      splitLine: {
        lineStyle: {
          type: 'dashed',
        },
      },
    },
    series: [
      {
        data: data,
        type: 'bar',
        barWidth: '30%',
        label: {
          show: true,
          position: 'top',
          formatter: '{c}%',
          color: color,
        },
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: color },
            { offset: 1, color: '#FFF' },
          ]),
        },
      },
    ],
  }
  if (option && typeof option === 'object') {
    myChart.setOption(option)
  }
  window.addEventListener('resize', myChart.resize)
}
